<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" name="" id="">

    <script>
        /**
         *  防抖：用户频繁触发的行为，只执行最后一次间隔一定时间的那一次
         * 
         */
        var oInput = document.querySelector('input');
        var timer = null;
        // oInput.onkeyup = function (e) {
        //     if (timer) {
        //         clearTimeout(timer);
        //     }
        //     timer = setTimeout(() => {
        //         console.log(this.value);
        //     }, 500);
        // }

        
        /**
         * 防抖：闭包
         * 定时器
         * 箭头函数 this arguments
         */
        function debounce(fn, delay) {
            var timer = null;
            return function () {
                if (timer) {
                    clearTimeout(timer);
                }
                timer = setTimeout(() => {
                    if(fn){
                        fn.apply(this,arguments);
                    }
                }, delay);
            }
        }

        oInput.onkeyup = debounce(function(e){
            console.log(this.value);
            console.log(e.target.value);
        }, 500);
    </script>
</body>

</html>